import scrapy
from Hue.basepro import ZhengFuBaseSpider
import re

class LiuzhouSpider(ZhengFuBaseSpider):
    name = 'Liuzhou-pro'
    allowed_domains = ['liuzhou.gov.cn']
    start_urls = ['http://www.liuzhou.gov.cn/']
    api = 'http://www.liuzhou.gov.cn/search/search?page={page}&channelid=269666&searchword={keyword}&keyword={keyword}&orderby=-DOCRELTIME&was_custom_expr=%28{keyword}%29&perpage=10&outlinepage=10&searchscope=&timescope=&timescopecolumn=&orderby=-DOCRELTIME&dates=&datee='
    keywords = ['碳管理']
    method = 'GET'

    def edit_page(self, response):
        page_info = response.xpath('//div[@class="sz123"]/a[@class="last-page"]/@href').get()
        page = re.search('search\?page=(.*?)&', page_info).group(1)
        return int(page)

    def edit_items_box(self, response):
        items_box = response.xpath('//div[@class="classify project"]/table')
        return items_box


    def edit_item(self, item):
        meta_info = {
            "title": item.xpath('//span[@class="js_zi2"]/a/text()').get(),
            "url": item.xpath('//span[@class="js_zi2"]/a/@href').get(),
            "date": item.xpath('//td[@class="js_zi1"]/text()').re(".*更新日期：(.*)")[0]
        }
        return meta_info

    def edit_article(self, response):
        article = {}
        article["source"] = response.css(".main_xTime::text").get()
        article["content"] = "".join(response.css("div.TRS_Editor *::text").getall())
        return article
